package com.pkk.spring.cloud.web.runner.controller.kafka.producer;

import com.pkk.spring.cloud.web.runner.controller.kafka.constant.KafkaConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @description: 简单的kafka生产消息
 * @author: peikunkun
 * @create: 2021-05-24 16:48
 **/
@Component
public class SimpleKafkaProducer {

    @Autowired
    private KafkaTemplate kafkaTemplate;


    /**
     * 发送kafka消息
     *
     * @param content:发送的内容
     * @return java.lang.Object：返回的错误建议
     * @author peikunkun
     * @date 2021/5/24 16:56
     */
    public String send(Object content) {
        AtomicReference<String> sendAdvice = new AtomicReference<>();
        kafkaTemplate.send(KafkaConstant.SIMPLE_TOPIC, content).addCallback(success -> {
        }, fail -> {
            sendAdvice.set(fail.getMessage());
        });
        return sendAdvice.get();
    }


}
